package nil.ed.livechat.timeshift.utils.m3u8;

import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;

import java.io.File;
import java.io.IOException;

/**
 * @author delin10
 * @since 2020/6/27
 **/
public class FastDfsUtils {

    private static final String URL_FORMAT = "http://180.76.247.245/livechat/file/download?bucket=%s&name=%s";


    static {
        try {
            ClientGlobal.init("fastdfs-client.properties");
        } catch (IOException | MyException e) {
            e.printStackTrace();
        }

    }

    public static String upload(File f) throws IOException, MyException {
        StorageClient client = getStorageClient();
        String[] rets = client.upload_file(f.getAbsolutePath(), null, null);
        return String.format(URL_FORMAT, rets[0], rets[1]);
    }

    private static StorageClient getStorageClient() throws IOException {
        return new StorageClient(ClientGlobal.getG_tracker_group().getTrackerServer(), null);
    }

}
